import Vue from 'vue'
import VueRouter from 'vue-router'
import versionUpdate from '@/update/versionUpdate.js';
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/cockpit',
    name: 'Cockpit',
    component: () => import('../views/cockpit.vue'),
  },
  {
    path: '/cockpit/dataDetail',
    name: 'dataDetail',
    component: () => import('../views/dataDetail.vue'),
  },
  {
    path: '/screen',
    name: 'screen',
    component: () => import('../views/screen.vue'),
  },
  {
    path: '/branch',
    name: 'branch',
    component: () => import('../views/branch.vue'),
  },
  {
    path: '/tradeStatistics',
    name: 'tradeStatistics',
    component: () => import('../views/tradeStatistics.vue'),
  },
  {
    path: '/screenHome',
    name: 'screenHome',
    component: () => import('../views/screenHome.vue'),
  },
  {
    path: '/test',
    name: 'test',
    component: () => import('../views/test.vue'),
  },
  {
    path: '/newScreen',
    name: 'newScreen',
    component: () => import('../views/newScreen.vue'),
  },
  {
    path: "/dataView",
    name: "dataView",
    component: () => import('../views/dataView.vue'),
  },
  //screen-data-list
  {
    path: "/screen-data-list",
    name: "screenDataList",
    component: () => import('../views/screen-data-list.vue'),
  },
  //新版数据大屏
  {
    path: "/dataScreen",
    name: "dataScreen",
    component: () => import('../views/dataScreen'),
  },
  {
    path: "/dataScreen/data-view",
    name: "dataView",
    component: () => import('../views/dataScreen/data-view.vue'),
  },
  {
    path: "/dataScreen/data-list",
    name: "dataList",
    component: () => import('../views/dataScreen/data-list.vue'),
  },
  {
    path: "/dataScreen/main-view",
    name: "mainView",
    component: () => import('../views/dataScreen/main-view.vue'),
  },
]

const router = new VueRouter({
  routes
});

//新页面返回顶部
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0)
});

router.beforeEach((to, from, next) => {
  /** 判断当前代码版本是否与服务器中代码版本一致，如不一致则刷新页面获取最新*/
  versionUpdate.isNewVersion();

  if (!sessionStorage.getItem("loginInfo")) {
    if (to.name == 'Login') {
      next()
    } else {
      next({ name: 'Login' })
    }
  }
  else {
    next()
  }
})

export default router
